package 力扣日常刷题.木23一月.第13天313;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author 帅小伙
 * @date 2022/3/13
 * @description
 */
public class Demo60 {


    public static void main(String[] args) {
        List<Integer> kDistantIndices = findKDistantIndices(new int[]{3, 4, 9, 1, 3, 9, 5}, 9, 1);

        for (Integer kDistantIndex : kDistantIndices) {
            System.out.print(kDistantIndex+" ");
        }
    }
    public static List<Integer> findKDistantIndices(int[] nums, int key, int k) {
        HashSet<Integer> set = new HashSet<>();
        int n = nums.length;

        for (int i = 0; i < n; i++) {
            if(nums[i] == key) {
                set.add(i);
            }
        }
        ArrayList<Integer> ans = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
                for (int integer : set) {
                    if(Math.abs(integer - i) <= k) {
                        ans.add(i);
                        break;
                    }
                }
        }
        return ans;
    }

}
